package com.fujin.gulimall.order.interceptor;

import com.fujin.common.Vo.MemberRsepVo;
import com.fujin.common.constant.AuthServerConstant;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @title: LoginUserInterceptor
 * @Author uIn
 * @Date: 2022/4/12 16:24
 * @Version 1.0
 * 拦截器
 * 订单请求需要登录过后才可以进行访问
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    ThreadLocal<MemberRsepVo> loginUser = new ThreadLocal<>();

    /**
     * 前置拦截器
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取登录session
        MemberRsepVo attribute = (MemberRsepVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute != null){
            //登录成功,存入共享数据中
            loginUser.set(attribute);
            return true;
        }else {
            request.getSession().setAttribute("msg","请先进行登录");
            //返回登录页
            response.sendRedirect("redirect:http://localhost:20000/login.html");
            return false;
        }
    }
}
